package cn.hls.web.controller.dict;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.SafetyCard;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.SafetyCardService;
import cn.hls.web.controller.base.BaseController;


/**
  *@fileName: SafetyCardController.java
  *@package: cn.hls.web.controller.dict
　*@ Description: 货物安全卡controller
　*@author: uuhui@163.com
  *@date: 2016年6月9日
 *
 */
@Controller
@RequestMapping("/safetyCard")
public class SafetyCardController extends BaseController<SafetyCard> {
	@Resource(name = "safetyCardService")
	private SafetyCardService service;

	/**
	 * 表单
	 * @param id
	 * @param show
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/info")
	public ModelAndView info(Long id, boolean show )
			throws Exception {
		SafetyCard entity = null;
		if (id != null) {
			entity = service.find(id);
		}
		if (show) {
			return new ModelAndView("dict/safetyCard_show").addObject("entity", entity); // 查看页面
		} else {
			return new ModelAndView("dict/safetyCard_info")
					.addObject("entity", entity);// 新增、修改页面
		}
	}
	
	/**
	 * 页面跳转
	 * 
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/view")
	public ModelAndView view() throws Exception {
		return new ModelAndView("dict/safetyCard_list");
	}

	/**
	 * 新增操作
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Msg add(SafetyCard entity) throws Exception {
		if (service.exists(entity)) {
			msg.setInfo("安全卡名称或说明书名称已经存在");
			msg.setStatus(201);
		} else {
			boolean flag = service.add(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(200);
			}
		}
		return msg;
	}

	/** 删除操作 **/
	@RequestMapping("/del")
	@ResponseBody
	public Msg del(String ids) throws Exception {
		String[] tempIds = ids.split("\\,");
		service.delete(tempIds);
		return msg;
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 *            分页对象
	 * @param entity
	 *            查询实时对象
	 * @return PageInfo<payTeam>
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<SafetyCard> list(PageInfo<SafetyCard> page, SafetyCard entity)
			throws Exception {
		return service.loadSafetyCardInfo(page, entity);
	}
	
	@RequestMapping("/loadSafetyCard")
	@ResponseBody
	public List<SafetyCard> loadSafetyCard(SafetyCard entity)throws Exception{
		return service.loadSafetyCardInfo(entity);
	}

	/**
	 * 更新操作
	 * 
	 * @param entity
	 *           货物类型对象
	 * @return Msg
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Msg update(SafetyCard entity) throws Exception {
		if (service.exists(entity)) {
			msg.setInfo("安全卡名称或说明书名称已经存在");
			msg.setStatus(201);
		} else {
			boolean flag = service.update(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(200);
			}
		}
		return msg;
	}
	@RequestMapping("/loadSafetyCardInfo")
	@ResponseBody
	public List<SafetyCard> loadSafetyCardInfo(SafetyCard entity)throws Exception{
		return service.loadSafetyCardInfo( entity);
	}
	
}
